package com.erp.staobaotrasg.activitysm;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.erp.staobaotrasg.R;
import com.erp.staobaotrasg.fragmentsm.IndexFragment;
import com.erp.staobaotrasg.fragmentsm.MakeMoneyFragment;
import com.erp.staobaotrasg.fragmentsm.PersonFragment;
import com.erp.staobaotrasg.fragmentsm.ReleaseFragment;
import com.erp.staobaotrasg.utils.TitleBarUtil;

public class MainFunctActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {
    public RadioGroup rg;
    FragmentManager fm;
    Fragment[] fragments;
    private FragmentTransaction ft;
    View pop_viewl;
    private ImageView iv_right;
    private PopupWindow popupWindow;//弹出框
    private TextView connect_we, tel_wel, f_con;

    RelativeLayout title_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_funct);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {
        title_main = (RelativeLayout) findViewById(R.id.RL_titlebar_background);
        //-------------------加载Fragment 的信息
        fragments = new Fragment[4];
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        if (savedInstanceState != null) {// 此方式为了解决横竖屏的切换重新加载数据
            int len = fragments.length;
            for (int i = 0; i < len; i++) {
                fragments[i] = fm.findFragmentByTag("Main_TAB_Menu" + i); //查找存在的标签
                ft.hide(fragments[i]);
            }
        } else {
            fragments[0] = IndexFragment.getInstance();// 首页界面
            fragments[1] = ReleaseFragment.getInstance();//社区的界面
            fragments[2] = MakeMoneyFragment.getInstance();//商城的界面
            fragments[3] = PersonFragment.getInstance();//个人的界面
            for (int i = 0; i < fragments.length; i++) {
                ft.add(R.id.fl_ma_menu, fragments[i], "Main_TAB_Menu" + i);// 首次添加 添加标识
                ft.hide(fragments[i]);// 添加后并进行隐藏
            }
        }
        ft.show(fragments[0]);
        ft.commit();//
        //--------------加载 视图的View 信息----------
        rg = (RadioGroup) findViewById(R.id.rg_ma_menu_select);//选择的 radiobutton 的按钮
        rg.setOnCheckedChangeListener(this);
        iv_right = (ImageView) findViewById(R.id.IV_titlebar_imgright);
        //---------pop 按钮
        if (popupWindow == null) {
            popupWindow = new PopupWindow(pop_viewl, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int TAB_POSITION = 0;
        String title = "有住网";
        switch (checkedId) {
            case R.id.rb_ma_menu_index:
                title = "有住网";
                TAB_POSITION = 0;
                break;
            case R.id.rb_ma_menu_diary:
                title = "装修日记";
                TAB_POSITION = 1;
                break;
            case R.id.rb_ma_menu_service:
                title = "服务";
                TAB_POSITION = 2;
                break;
            case R.id.rb_ma_menu_mine:
                title = " ";
                TAB_POSITION = 3;
                break;
        }
        switchMFragment(title, TAB_POSITION);
    }

    public void switchMFragment(String title, int TAB_POSITION) {
        //  TitleBarUtil.setFragmentATitleBar(MainV310Activity.this, title, false, R.drawable.index_m_more);
        ft = fm.beginTransaction();
        for (int i = 0; i < fragments.length; i++) {
            if (i == TAB_POSITION) {
                ft.show(fm.findFragmentByTag("Main_TAB_Menu" + i));

            } else {
                ft.hide(fm.findFragmentByTag("Main_TAB_Menu" + i));
            }
        }
        if (TAB_POSITION == 1 || TAB_POSITION == 2) {
            title_main.setVisibility(View.GONE);
        } else {
            title_main.setVisibility(View.VISIBLE);
        }
        ft.commit();
    }
}
